/**
 * http://ghj.nc.gov.cn/
 * 南昌规划局信息爬虫
 * sunny/acc 2017-06-28
 */


const utils = require("../../utils");
import { Base, Item } from "../base";

class TempClass {
    name: string;
    url: string;
}

class ncghjxx_spider extends Base {
    firstPage: string;
    constructor() {
        super();
        this.name = "南昌规划局信息";
        this.encode = "utf-8";
        this.debug = false;
        this.timeout = 1000000;
        this.collection = "ncghjxx";
    }

    getPageCount(): number {

        var content: string = this.spider.getSync("http://ghj.nc.gov.cn/ydghxkz/index.jhtml");
        var $: CheerioSelector = this.cheerio.load(content);
        // var num = $(".page").children("select").children("option").eq(-1).text();
        var num = $(".page select option").eq(-1).text().trim();
        // console.log("num2:" + num);
        var cnt : number = parseInt(num);
        this.firstPage = content;
        return cnt;
    }

    getItems(pageIndex: number): Item[] {
        var result: Item[] = [];
        var content: string;
        var that = this;
        if (pageIndex == 0)
            content = this.firstPage;
        else {
            let url = "http://ghj.nc.gov.cn/ydghxkz/index_" + (pageIndex + 1) + ".jhtml";
            content = this.spider.getSync(url);
        }
        var $: CheerioSelector = this.cheerio.load(content);
        let self = this;
        $(".tableShow table tr.tr2").each(function (index, ele) {
            let item: Item = self.buildItem();
            var bh: string = $(ele).find("td.tr2").eq(0).children("div").children("a").text();
            (item as any)["编号"] = bh;
            var xmmc: string = $(ele).find("td.tr2").eq(1).children("div").children("a").text();
            item.title = xmmc;
            var sbdw: string = $(ele).find("td.tr2").eq(2).children("div").text();
            (item as any)["申报单位"] = sbdw;
            var fzrq: string = $(ele).find("td.tr2").eq(3).children("div").text();
            (item as any)["发证日期"] = fzrq;
            var bhUrl = "http://ghj.nc.gov.cn" + $(ele).find("td.tr2").eq(0).children("div").children("a").attr("href");
            if (bhUrl) {
                that.processItems(bhUrl, bh, item);
            }
            result.push(item);
        });
        return result;
    }
    //获取最后一级明细
    private processItems(bhUrl: string, bh: string, item: Item): void {
        var self = this;
        var content: string = self.spider.getSync(bhUrl);
        // console.log("content :" + bhUrl);
        var $: CheerioSelector = self.cheerio.load(content);
        (item as any)["url"] = bhUrl;
        $(".dshu table ").each(function (index, ele) {
            // (item as any)["编号"] = $(ele).find("tr").eq(0).children("td").children("span").text().trim();//编号        
            (item as any)["用地单位"] = $(ele).find("tr").eq(2).children("td").children("span").text().trim();//用地单位
            (item as any)["用地项目名称"] = $(ele).find("tr").eq(3).children("td").children("span").text().trim();//用地项目名称
            (item as any)["用地性质"] = $(ele).find("tr").eq(4).children("td").children("span").text().trim();//用地性质
            (item as any)["用地面积"] = $(ele).find("tr").eq(5).children("td").children("span").text().trim();//用地面积
            var jsgm: string = $(ele).find("tr").eq(6).children("td").children("span").text().trim();//建设规模
            if (jsgm)
                (item as any)["建设规模"] = jsgm;//建设规模
            else {
                (item as any)["建设规模"] = "无";
            }
            (item as any)["附图及附图名称"] = $(ele).find("tr").eq(7).children("td").children("span").text().trim();//附图及附图名称
        });
    }
}
//将爬到的数据传到dist里面
export = new ncghjxx_spider();